package com.lib724.web;

import javax.servlet.ServletContext;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.name.Names;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.javaeedev.lightweight.mvc.Action;
import com.javaeedev.lightweight.mvc.ExceptionResolver;
import com.javaeedev.lightweight.mvc.ServletContextAware;
import com.javaeedev.lightweight.mvc.ViewResolver;
import com.javaeedev.lightweight.mvc.view.JspViewResolver;
import com.lib724.dao.FacadeImpl;
import com.lib724.dao.SqlMapClientP;
import com.lib724.dao.impl.Facade;
import com.lib724.web.controller.IndexController;
import com.lib724.web.controller.UserController;

public class Lib724Module implements Module, ServletContextAware {

    protected ServletContext context;

    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    @SuppressWarnings("unchecked")
    public void configure(Binder binder) {
    	binder.bind(SqlMapClient.class).toProvider(SqlMapClientP.class).in(Scopes.SINGLETON);
    	binder.bindConstant().annotatedWith(Names.named("ibatis.configXml")).to("../sql-map-config.xml");
        // bind facade:
        binder.bind(Facade.class).to(FacadeImpl.class).asEagerSingleton();
//
        // bind interceptors:
        binder.bind(TransactionInterceptor.class).asEagerSingleton();

        // bind ViewResolver:
        binder.bindConstant().annotatedWith(Names.named("p:prefix")).to("/WEB-INF/jsp/");
        binder.bindConstant().annotatedWith(Names.named("p:suffix")).to(".jsp");        
        binder.bind(ViewResolver.class).to(JspViewResolver.class).asEagerSingleton();
        // bind ExceptionResolver:
        binder.bind(ExceptionResolver.class).to(ExceptionHandler.class).asEagerSingleton();

        // bind dao:
    	// binder.bind(MyDao.class).to(MyDaoImpl.class).in(Scopes.SINGLETON);

        // bind actions:
        final String c = this.context.getContextPath();
        binder.bind(Action.class).annotatedWith(Names.named(c + "/index.do")).to(IndexController.class).asEagerSingleton();
        binder.bind(Action.class).annotatedWith(Names.named(c + "/user.do")).to(UserController.class).asEagerSingleton();
    }

}
